<?php

namespace App\Http\Controllers\Api;

use App\Model\GoldCardLog;
use Illuminate\Support\Facades\Storage;
use App\Model\UserModel;
use Illuminate\Http\Request;
use App\Model\StoreModel;
use App\Model\ConfigModel;
use App\Model\UserWalletModel;
use App\Services\Msfl;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Yansongda\Pay\Log;


class VipController extends Controller
{
	//vip首页
	public function index(){
		//用户信息
		$data['user'] = UserModel::Dolist(request()->uid);
		$data['vip_one'] = ConfigModel::getValue('vip_one');
		$data['vip_two'] = ConfigModel::getValue('vip_two');
		return ['code'=>1,'data'=>$data];
	}

	//成为VIP
	public function do_check(){
		if(!request()->type){
			return ['code'=>0,'msg'=>retStrInfo(91)];
		}
		//用户信息
		$user = UserModel::Dolist(request()->uid);
		//二级密码是否正确
		if (!Hash::check(request()->paypass, $user['paypass'])) {
			return ['code' => 0, 'msg' => retStrInfo(56)];
		}

		if($user['is_auth'] != 1){
			return ['code'=>0,'msg'=>retStrInfo(95)];
		}
		$vip_one = ConfigModel::getValue('vip_one');
		$vip_two = ConfigModel::getValue('vip_two');
		$level_one_ratio = ConfigModel::getValue('level_one_ratio');

		$level_two_ratio = ConfigModel::getValue('level_two_ratio');
		$wallet = UserWalletModel::where('uid',request()->uid)->first();
		if(!$wallet){
			return ['code'=>0,'msg'=>retStrInfo(121)];
		}
		DB::beginTransaction();
		try{
			if(StoreModel::where('uid',request()->uid)->first() && StoreModel::where('uid',request()->uid)->value('is_end') == 1){
				return ['code'=>0,'msg'=>retStrInfo(92)];
			}
			switch(request()->type){
				//388
				case '1':
					if($wallet->money < $vip_one){
						return ['code'=>0,'msg'=>retStrInfo(90)];
					}
					//开启云仓库,判断是否有过云仓库,有过云仓库并且过期,修改已有仓库的开始时间和结束时间,更改状态(is_end)
                    if(StoreModel::where('uid',request()->uid)->first()){
                        $store = StoreModel::where('uid',request()->uid)->update([
                            'start' => date('Y-m-d H:i:s'),
                            'end' => date('Y-m-d H:i:s',strtotime('+365 days')),
                            'is_end' => 1
                        ]);
                        $store_id = StoreModel::where('uid',request()->uid)->value('id');
                    }else{

                        $store = StoreModel::create([
                            'uid' => request()->uid,
                            'start' => date('Y-m-d H:i:s'),
                            'end' => date('Y-m-d H:i:s',strtotime('+365 days')),
                            'state' => 1,
                            'type' => 1,
                        ]);
                        $store_id = $store->id;
                    }

					//增加日志
					UserWalletModel::handleAccount(request()->uid,$vip_one,'-','开通VIP',1,4,'store',$store_id);

					break;
				//支付10000货款
				case '2':
					if($wallet->money < $vip_two){
						return ['code'=>0,'msg'=>retStrInfo(90)];
					}
					//开启云仓库
                    if(StoreModel::where('uid',request()->uid)->first()){
                        $store = StoreModel::where('uid',request()->uid)->update([
                            'start' => date('Y-m-d H:i:s'),
                            'end' => date('Y-m-d H:i:s',strtotime('+365 days')),
                            'is_end' => 1,
                        ]);
                        $store_id = StoreModel::where('uid',request()->uid)->value('id');
                    }else{
                        $store = StoreModel::create([
                            'uid' => request()->uid,
                            'start' => date('Y-m-d H:i:s'),
                            'end' => date('Y-m-d H:i:s',strtotime('+365 days')),
                            'state' => 1,
                            'type' => 1,
                        ]);
                        $store_id = $store->id;
					}
					$msfl = new Msfl();
					//增加个人业绩
					$msfl->performance(request()->uid,$vip_two);
					//增加日志
					UserWalletModel::handleAccount(request()->uid,$vip_two,'-','开通VIP',1,4,'store',$store_id);
					UserWalletModel::handleAccount(request()->uid,$vip_two,'+','开通VIP',2,4,'store',$store_id);
					break;
			}
			//用户信息
			$user = UserModel::Dolist(request()->uid);
			if($user['is_auth'] == 1){
				//升级成为银级
				UserModel::where('id',request()->uid)->update(['level'=>1]);
				//一代
				$puser = UserModel::where('id',$user['pid'])->first();
				if($puser['level'] > 0){
					//一代二代赠送货款
					UserWalletModel::handleAccount($puser['id'],$level_one_ratio,'+','创客奖',2,12,'user',$user['id']);
				}

				//二代
				$tuser = UserModel::where('id',$puser['pid'])->first();

				if($tuser['level'] > 0){
					//一代二代赠送货款
					UserWalletModel::handleAccount($tuser['id'],$level_two_ratio,'+','创客奖',2,12,'user',$user['id']);
				}
			}
			UserModel::where('id',request()->uid)->update(['is_vip'=>1]);
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(93)];
		}catch(Exception $e){
			DB::rollBack();
			throw $e;
			return ['code'=>0,'msg'=>retStrInfo(94)];
		}

	}

    public function jl_ceshi(){
        $uid = 40;
        $price = 100;
        $lj_rate = 0;
        $this->sales_incentive($uid,$price,$lj_rate);
    }
	public function sales_incentive($uid,$price,$lj_rate = 0){
            $pid = UserModel::where('id',$uid)->value('pid');
//            dd($pid);
            if($pid == 0){
                \Illuminate\Support\Facades\Log::info('查到顶级,结束查找');
                return;
            }
            if($lj_rate >= 0.195){
                \Illuminate\Support\Facades\Log::info('比例达到最大,停止发奖');
                return;
            }
            //获取上级所得销售奖比例
            $puser = UserModel::where('id',$pid)->first();
            if($puser->level > 0){
                if($puser->level == 1){
                    $puser_yj_rate = ConfigModel::getValue('person_yj_rate');
                }elseif($puser->level == 2){
                    $all_yj = bcadd($puser->person_yj,$puser->team_yj,5);
                    $duan = bcsub($all_yj,2000,5);
                    if($duan <= 0){
                        $puser_yj_rate = ConfigModel::getValue('person_yj_jin_rate');
                    }else{
                        $num = floor(bcdiv($duan,3000,5));//有几个3000,一个3000多1.5%
                        if($num>=8){
                            $num = 8;
                        }
                        $puser_yj_rate = ConfigModel::getValue('person_yj_jin_rate')+($num*0.015);
                    }
                }

                //如果上级销售奖比例大于下级则有领导奖否则没有
                if($puser_yj_rate > $lj_rate){
                    $rate = $puser_yj_rate - $lj_rate;
                    $reword = bcmul($price,$rate,5);
                    /*
                     * uid 用户id
                     * sxj 奖金数额
                     * msg 描述
                     * type 1现金 2货款
                     * status 消费类型(type=1 1充值 2提现 3转货款 4成为VIP 5销售奖 6领导奖 7教育奖 8云仓库收益 9后台充值  type=2 3转货款 10入库 11下单 12创客奖 13累计充值够10000返利388)
                     * dy_table 对应表
                     * dy_id  报单用户id
                     */
                    UserWalletModel::handleAccount($pid,$reword,'+','领导奖',1,6,'',$uid);
                    return $this->sales_incentive($pid,$price,$puser_yj_rate);
                }else{
                    return $this->sales_incentive($pid,$price,$lj_rate);
                }
            }
            return $this->sales_incentive($pid,$price,$lj_rate);
    }



}